package com.link.play.fit.ai.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatTextView;

public class ArcTextView extends AppCompatTextView {

    private static final int STATE_DASHED = 0; // 虚线圆
    private static final int STATE_SOLID = 1;  // 实线圆圈
    private static final int STATE_THREE_QUARTER = 2; // 四分之三圆弧
    private static final int STATE_FOUR_NO = 3; // 不画线

    private int currentState = STATE_DASHED; // 默认状态
    private Paint arcPaint;
    private RectF arcRect;
    private int colorStatus = 0;
    public ArcTextView(Context context) {
        super(context);
        init();
    }

    public ArcTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ArcTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setStrokeWidth(2); // 圆弧宽度
        arcPaint.setStyle(Paint.Style.STROKE);
        arcRect = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 设置圆弧绘制的矩形区域
        int padding = 10; // 圆弧与文本的间距
        arcRect.set(padding, padding, getWidth() - padding, getHeight() - padding);
        switch (colorStatus){
            case 0:
                arcPaint.setColor(0xFF4E4E4E); //灰色
                break;
            case 1:
                arcPaint.setColor(0xFFFA3030); //红色
                break;
            case 2:
                arcPaint.setColor(0xff30FA3D); //绿色
                break;
        }
        // 根据当前状态绘制圆弧
        switch (currentState) {
            case STATE_DASHED:
                arcPaint.setPathEffect(new android.graphics.DashPathEffect(new float[]{10, 5}, 0)); // 虚线效果
                canvas.drawOval(arcRect, arcPaint);
                break;
            case STATE_SOLID:
                arcPaint.setPathEffect(null); // 实线效果
                canvas.drawOval(arcRect, arcPaint);
                break;
            case STATE_THREE_QUARTER:
                arcPaint.setPathEffect(null); // 实线效果
                canvas.drawArc(arcRect, -90, 270, false, arcPaint); // 绘制四分之三圆弧
                break;
            case STATE_FOUR_NO:
                // 清除画布
//                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//                arcPaint.setPathEffect(null); // 不画线
//                canvas.drawArc(arcRect, -90, 270, false, arcPaint); // 绘制四分之三圆弧
                break;
        }
    }

    /**
     * 设置当前状态
     *
     * @param state 0: 虚线圆, 1: 实线圆圈, 2: 四分之三圆弧
     */
    public void setState(int state) {
        if (state < 0 || state > 3) {
            throw new IllegalArgumentException("Invalid state value");
        }
        this.currentState = state;
        invalidate(); // 刷新视图
    }

    public void setColorStatus(int state){
     this.colorStatus = state;
     invalidate();
    }
}

